package com.example.demo2.controller;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.service.EquipmentService;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.vo.CategoryVO;
import com.example.demo2.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/** 
 * <br/>
 * Created by weidong on 2020/02/16
 */
@Controller
public class CategoryController {

	@Resource
	private CategoryService categoryService;
	@Resource
	private EquipmentService equipmentService;

	@RequestMapping("/category_list")
	public String apartment(String name, @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
		CategoryVO categoryVO = new CategoryVO();
		categoryVO.setType(name);
		categoryVO.setCurrentPage(currentPage);
		PageBean pageBean=categoryService.listPage(categoryVO);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("query", categoryVO);
		return "category_list";
	}
	@RequestMapping("/category_list/add")
	@ResponseBody
    //添加
	public Result add(String typename)throws Exception{
		CategoryVO categoryVO=new CategoryVO();
		categoryVO.setType(typename);
		return categoryService.add(categoryVO);
	}

	@RequestMapping("/category_list/delete/{id}")
	@ResponseBody
	//删除
	public Result deleteById(@PathVariable("id") Integer id)throws Exception{
        Result result=equipmentService.deleteByCategoryId(id);
		Integer code = result.getCode();
		if (code==200){
			categoryService.deleteById(id);
		}
		return result;
	}

	@RequestMapping("/category_list/update")
	@ResponseBody
	//更新
	public Result updateById(Integer id,String typename2)throws Exception{
		CategoryVO categoryVO=new CategoryVO();
		categoryVO.setId(id);
		categoryVO.setType(typename2);
		return categoryService.updateById(categoryVO);
	}
    @RequestMapping("/category_list/find/{id}")
    @ResponseBody
	//更新回显
    public Result findClass(@PathVariable("id")Integer id)throws Exception{
        return categoryService.findById(id);
    }


}